#!/usr/bin/env python3
"""
创建专业的LimeReader应用图标
根据详细设计方案：专业、简洁、有深度的文档查看器形象
"""

from PIL import Image, ImageDraw
import math
import os

def create_squircle_mask(size, corner_radius_ratio=0.22):
    """创建Apple风格的超椭圆(Squircle)蒙版"""
    mask = Image.new('L', (size, size), 0)
    draw = ImageDraw.Draw(mask)
    radius = int(size * corner_radius_ratio)
    draw.rounded_rectangle([0, 0, size-1, size-1], radius=radius, fill=255)
    return mask

def create_professional_icon(size):
    """创建符合设计方案的专业图标"""
    # 创建透明画布
    img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)

    # 1. 背景 - 深沉的石板蓝色
    bg_color = (70, 90, 120, 255)  # 石板蓝

    # 创建超椭圆背景
    mask = create_squircle_mask(size, 0.22)
    bg_img = Image.new('RGBA', (size, size), bg_color)
    bg_img.putalpha(mask)

    # 2. 文档设置
    margin = size * 0.15
    doc_width = size * 0.5
    doc_height = size * 0.65
    doc_x = (size - doc_width) // 2
    doc_y = size * 0.12

    # 3. 绘制辅助文档（背景层）
    aux_offset_x = size * 0.03
    aux_offset_y = size * 0.03
    aux_coords = [
        doc_x + aux_offset_x,
        doc_y + aux_offset_y,
        doc_x + doc_width + aux_offset_x,
        doc_y + doc_height + aux_offset_y
    ]

    # 辅助文档阴影
    shadow_coords = [aux_coords[0] + 2, aux_coords[1] + 2, aux_coords[2] + 2, aux_coords[3] + 2]
    draw.rounded_rectangle(shadow_coords, radius=3, fill=(0, 0, 0, 40))

    # 辅助文档主体
    draw.rounded_rectangle(aux_coords, radius=3, fill=(245, 245, 245, 255))
    draw.rounded_rectangle(aux_coords, radius=3, outline=(200, 200, 200, 120), width=1)

    # 4. 绘制主文档
    main_coords = [doc_x, doc_y, doc_x + doc_width, doc_y + doc_height]

    # 主文档阴影
    main_shadow = [main_coords[0] + 3, main_coords[1] + 3, main_coords[2] + 3, main_coords[3] + 3]
    draw.rounded_rectangle(main_shadow, radius=4, fill=(0, 0, 0, 60))

    # 主文档主体
    draw.rounded_rectangle(main_coords, radius=4, fill=(255, 255, 255, 255))
    draw.rounded_rectangle(main_coords, radius=4, outline=(210, 210, 210, 180), width=1)

    # 5. 页角卷起效果
    corner_size = size * 0.06
    corner_x = main_coords[2] - corner_size
    corner_y = main_coords[1]
    corner_points = [
        (main_coords[2], main_coords[1]),
        (corner_x, main_coords[1]),
        (main_coords[2], corner_y + corner_size)
    ]
    draw.polygon(corner_points, fill=(235, 235, 235, 255))
    draw.polygon(corner_points, outline=(190, 190, 190, 150), width=1)

    # 6. 文档内容 - 双栏文字
    content_margin = size * 0.04
    content_x = doc_x + content_margin
    content_y = doc_y + content_margin * 1.5
    content_width = doc_width - 2 * content_margin

    # 计算栏宽
    column_gap = size * 0.025
    column_width = (content_width - column_gap) // 2

    # 文字颜色
    text_color = (120, 120, 120, 255)
    title_color = (80, 80, 80, 255)

    # 行设置
    line_height = size * 0.02
    line_spacing = size * 0.01

    # 左栏
    left_x = content_x

    # 左栏标题
    title_height = size * 0.025
    draw.rectangle([left_x, content_y, left_x + column_width * 0.7, content_y + title_height], fill=title_color)

    # 左栏文字行
    current_y = content_y + title_height + line_spacing * 2
    for i in range(8):
        if current_y + line_height > main_coords[3] - content_margin:
            break
        line_width = column_width * (0.75 + 0.2 * (i % 3) / 3)
        draw.rectangle([left_x, current_y, left_x + line_width, current_y + line_height], fill=text_color)
        current_y += line_height + line_spacing

    # 右栏
    right_x = content_x + column_width + column_gap

    # 右栏标题
    draw.rectangle([right_x, content_y, right_x + column_width * 0.6, content_y + title_height], fill=title_color)

    # 右栏文字行
    current_y = content_y + title_height + line_spacing * 2
    for i in range(8):
        if current_y + line_height > main_coords[3] - content_margin:
            break
        line_width = column_width * (0.7 + 0.25 * (i % 4) / 4)
        draw.rectangle([right_x, current_y, right_x + line_width, current_y + line_height], fill=text_color)
        current_y += line_height + line_spacing

    # 7. 侧边指示器
    indicator_count = 3
    indicator_width = size * 0.025
    indicator_height = size * 0.015
    indicator_spacing = size * 0.012
    indicator_color = (200, 110, 70, 255)  # 柔和的橙色

    # 指示器位置
    indicators_height = indicator_count * indicator_height + (indicator_count - 1) * indicator_spacing
    indicator_start_y = doc_y + (doc_height - indicators_height) // 2
    indicator_x = doc_x - indicator_width - size * 0.008

    # 绘制指示器
    for i in range(indicator_count):
        y_pos = indicator_start_y + i * (indicator_height + indicator_spacing)
        draw.rounded_rectangle([
            indicator_x, y_pos,
            indicator_x + indicator_width, y_pos + indicator_height
        ], radius=1, fill=indicator_color)

    # 合并背景和内容
    final_img = Image.alpha_composite(bg_img, img)

    return final_img

def create_all_sizes():
    """生成所有尺寸的图标"""
    sizes = [16, 32, 48, 64, 128, 256, 512]
    icons_dir = "/Users/ethan/Codes/longimagereader/icons"

    print("开始生成专业图标...")

    # 生成512px主图标
    main_icon = create_professional_icon(512)
    main_icon.save(os.path.join(icons_dir, "app.png"), "PNG", optimize=True)
    print("✓ 生成主图标 app.png (512x512)")

    # 生成其他尺寸
    for size in sizes:
        if size == 512:
            icon = main_icon
        else:
            icon = main_icon.resize((size, size), Image.Resampling.LANCZOS)

        filename = f"app_{size}x{size}.png"
        icon.save(os.path.join(icons_dir, filename), "PNG", optimize=True)
        print(f"✓ 生成 {filename}")

    print("\n✅ 所有PNG图标生成完成！")
    return main_icon

if __name__ == "__main__":
    create_all_sizes()